//
//  SetItemCell.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/10.
//

import UIKit

class SetItemSwitchCell: UITableViewCell {
    weak var delegate:SetItemCellDelegate?
    private var indexPath:IndexPath?
    private var checked:UISwitch = UISwitch()
    private var titleLabel:UILabel = UILabel()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.centerY.equalToSuperview()
        })
        titleLabel.font  = Font_Size_14
        contentView.addSubview(checked)
        checked.snp.makeConstraints({
            $0.centerY.equalToSuperview()
            $0.right.equalToSuperview().inset(10)
        })
        checked.addTarget(self, action: #selector(switchClick), for: .valueChanged)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func switchClick(){
        if delegate != nil {
            delegate!.swithcValueChanged!(indexPath! )
        }
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
        
    }
    func binding(_ data:SetItemModel,indexPath:IndexPath) {
        self.indexPath = indexPath
        checked.isOn = data.isOn
        self.titleLabel.text = data.title
        self.titleLabel.textColor = data.titleColor

        if(data.subTitle != ""){
            let text = data.title + "\n" + data.subTitle
            self.titleLabel.attributedText = text.getAttributedString(data.subTitle, subColor: data.subTitleColor, subFont: Font_Size_12, lineSpace: 5.0)
            titleLabel.numberOfLines = 2
        }else{
            titleLabel.numberOfLines = 1
        }
        self.selectionStyle = .none
    }
    deinit {
        delegate = nil
    }
}


